﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FPModel;

namespace FacebookPoster
{
    public class FBPagePosts
    {
        public static List<FBPagePost> Get()
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    return context.FBPagePosts.ToList();
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }

        public static FBPagePost GetByID(int id)
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    return context.FBPagePosts.FirstOrDefault(us => us.ID == id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }

        public static FBPagePost GetByPageToken(string pageNo)
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    return context.FBPagePosts.FirstOrDefault(us => us.PageNo.ToLower() == pageNo.ToLower());
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }

        public static int Add(FBPagePost pagePost)
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    context.AddToFBPagePosts(pagePost);
                    context.SaveChanges();

                    return 1;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
        }

        public static int Update(int id, string newName)
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    FBPagePost pageEdit = new FBPagePost();
                    pageEdit = (from c in context.FBPagePosts where c.ID == id select c).First();
                    pageEdit.Name = newName;
                    context.SaveChanges();

                    return 1;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
        }

        public static int Delete(int id)
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    FBPagePost pageEdit = new FBPagePost();
                    pageEdit = (from c in context.FBPagePosts where c.ID == id select c).First();

                    context.DeleteObject(pageEdit);
                    context.SaveChanges();

                    return 1;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
        }

    }
}